﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class admintractor_Article : System.Web.UI.Page
{
    ArticleBusiness article = new ArticleBusiness();
    CategoryBusiness c = new CategoryBusiness();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bindDrop();
            bindGV();
        }
    }

    public void bindGV()
    {
        GridView1.DataSource = article.getArticle();
        GridView1.DataBind();
    }

    public string viewCategory(int category_id)
    {
        string tmp = "";
        ArrayList items = c.getCategory();
        CategoriesEntities item = null;
        for (int i = 0; i < items.Count; i++)
        {
            item = (CategoriesEntities)items[i];
            if (item._id == category_id)
            {
                tmp = item._name;
                break;
            }
        }
        return tmp;
    }


    public string view(string _status)
    {
        string tmp = "";
        if (_status.Equals("1") || _status.Equals("True"))
        {
            tmp = "Hiển thị";
        }
        else
        {
            tmp = "Không";
        }

        return tmp;
    }

    public void bindDrop()
    {
        PanelBusiness p = new PanelBusiness();
        ArrayList items = p.getPanel_Category(1);
        DropDownList1.DataSource = items;
        DropDownList1.DataTextField = "_name";
        DropDownList1.DataValueField = "_id";
        DropDownList1.DataBind();
        DropDownList1.Items.Add(new ListItem("- Tất cả -", 0.ToString()));
        DropDownList1.SelectedValue = 0.ToString();
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("ArticleEdit.aspx");
    }

    protected void lbEdit_Click(object sender, EventArgs e)
    {
        Response.Redirect("ArticleEdit.aspx?id=" + ((LinkButton)sender).CommandArgument);
    }
    protected void lbDelete_Click(object sender, EventArgs e)
    {
        if (article.delete(int.Parse(((LinkButton)sender).CommandArgument)))
        {
            Response.Redirect("Article.aspx");
        }
        else
        {
            lb_Error.Text = "No delete";
        }
    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        int id = int.Parse(DropDownList1.SelectedValue.Trim());
        if (id > 0)
        {
            GridView1.DataSource = article.getArticle_panel(id);
            GridView1.DataBind();
        }
        else
        {
            bindGV();
        }
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        int id=int.Parse(DropDownList1.SelectedValue.Trim());
        if (id > 0)
        {
            GridView1.DataSource = article.getArticle_panel(id);
            GridView1.DataBind();
        }
        else
        {
            bindGV();
        }
    }
}
